import requests
from urllib.parse import quote
from db_save import SaveData


class GetInfo(object):

    def __init__(self, keyword):
        self.headers = {
            "Host": "jjhygl.hzfc.gov.cn",
            "Referer": "http://jjhygl.hzfc.gov.cn/wx/gpfyselectlist.html?keywords={}".format(quote(keyword)),
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
        }
        # 列表页url
        self.base_url_list = "http://jjhygl.hzfc.gov.cn/webty/WxAction_getGpxxSelectList.jspx?page={}&keywords=" + quote(
            keyword)
        # 详情页url
        self.base_url_detail = "http://jjhygl.hzfc.gov.cn/webty/WxAction_toGpxxInfo.jspx?gpfyid={}"
        # 详情页图片url
        self.base_url_image = "http://jjhygl.hzfc.gov.cn/memty/MemAction_selectFwytxxList.jspx?gpfyid={}"
        # 创建MySQL连接池
        self.pool = SaveData()

    def get_gpfyid(self):
        """
        获取挂牌房源id: gpfyid
        """

        page = 1
        while True:
            url = self.base_url_list.format(str(page))
            respons = requests.get(url=url, headers=self.headers)
            content = respons.json()
            info_list = content.get('list')
            is_over = content.get('isover')
            if info_list:
                for info in info_list:
                    gpfyid = info.get("gpfyid")
                    yield gpfyid
            if is_over:
                break
            page += 1

    def get_detail_content(self, gpfyid):
        """获取挂牌房源id对应的房源详细信息
        Arguments:
            gpfyid {[int]} -- [挂牌房源id]
        Returns:
            [type] -- [调用解析函数，保存数据]
        """

        detail_url = self.base_url_detail.format(str(gpfyid))
        self.headers["Referer"] = "http://jjhygl.hzfc.gov.cn/wx/gpfyselectinfo.html?gpfyid={}".format(str(gpfyid))
        detail_respons = requests.get(url=detail_url, headers=self.headers)
        detail_content = detail_respons.json()
        return self.parse_detail_content(detail_content)

    def get_image_content(self, gpfyid):
        image_url = self.base_url_image.format(str(gpfyid))
        self.headers["Referer"] = "http://jjhygl.hzfc.gov.cn/wx/gpfyselectinfo.html?gpfyid={}".format(str(gpfyid))
        image_respons = requests.get(url=image_url, headers=self.headers)
        image_content = image_respons.json()
        return self.parse_image_content(image_content)

    def parse_detail_content(self, detail_content):
        item = {}
        if detail_content:
            gpxx = detail_content.get("gpxx")
            item["cqmc"] = gpxx.get("cqmc")
            item["cyrybh"] = gpxx.get("cyrybh")
            item["fczsh"] = gpxx.get("fczsh")
            item["fwtybh"] = gpxx.get("fwtybh")
            item["fwytValue"] = gpxx.get("fwytValue")
            item["gpfyid"] = gpxx.get("gpfyid")

            item["gplxrdh"] = gpxx.get("gplxrdh")
            item["gplxrxm"] = gpxx.get("gplxrxm")
            item["gpztValue"] = gpxx.get("gpztValue")
            item["hxs"] = gpxx.get("hxs")
            item["hxt"] = gpxx.get("hxt")
            item["hxw"] = gpxx.get("hxw")

            item["jzmj"] = gpxx.get("jzmj")
            item["szlc"] = gpxx.get("szlc")
            item["szlcname"] = gpxx.get("szlcname")
            item["wtcsjg"] = gpxx.get("wtcsjg")
            item["xqmc"] = gpxx.get("xqmc")
            item["xzqhname"] = gpxx.get("xzqhname")
        if item:
            self.pool.save(item)

    def parse_image_content(self, image_content):
        image_list = image_content.get("list")
        if image_list:
            item = {}
            for i in image_list:
                item["gpfyid"] = i.get("gpfyid")
                item["tpcflj"] = i.get("tpcflj")
                item["tplx"] = i.get("tplx")
            if item:
                self.pool.save(item)


if __name__ == "__main__":
    # 测试
    info = GetInfo("文一路小区")
    gpfyids = info.get_gpfyid()
    for gpfyid in gpfyids:
        print("+"*50)
        detail = info.get_detail_content(gpfyid)
        image = info.get_image_content(gpfyid)
        info.parse_detail_content(detail)
        info.parse_image_content(image)
